<?php
namespace app\hotback\controller;
use think\Db;
class Basedata extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 数据列表
	 */
	public function index()
	{
	    $usercount=db::name('user')->count();
	    
	    $user_old=db::query('select count(id) as count from jx_user where id in (select user_id from jx_order where 1=1 )')[0]['count'];
	    
	    $user_new=$usercount-$user_old;
	    
	    
	    $service=db::name('service')->where('is_delete_time','0')->select();
	    
	    foreach ($service as $k=>$v){
	        
	        $service[$k]['total']=db::query('select sum(price*buy_number) as sums from jx_order_detail where showtype=1 and service_id="'.$v['id'].'" and order_id in (select id from jx_order where status=6 )')[0]['sums'];
	        if(empty($service[$k]['total'])){
	            $service[$k]['total']=0;
	        }
	    }
	    
	    $address=db::name('order_address')->where('county','<>','')->field('*,CONCAT(province,city,county) as concats')->group('concats')->select();
	    
	    $address_data=[];
	    
	    foreach ($address as $k=>$v){
	        $address_data[$k]['title']=$v['province_name'].$v['city_name'].$v['county_name'];
	        $address_data[$k]['num']=db::name('order_address')->where(['province'=>$v['province'],'city'=>$v['city'],'county'=>$v['county']])->count();
	    }
	    
	    $from=db::name('order_from')->group('title')->column('title');
	    
		return $this->fetch('',[
		    'user_old'=>$user_old,
		    'user_new'=>$user_new,
		    'service'=>$service,
		    'address_data'=>$address_data,
		    'from'=>implode(',',$from)
		    ]);
	}

    
    public function techn(){
        $list=db::query("select *,(select sum(pay_price)  from jx_order where jx_order.techn_id=jx_techn.id and jx_order.status=6) as sums from jx_techn   order by sums desc limit 20");
        
        return $this->fetch('',[
		    'list'=>$list
		    ]);
        
    }
    
    public function user(){
        $list=db::query("select *,(select sum(pay_price)  from jx_order where jx_order.user_id=jx_user.id and jx_order.status=6) as sums from jx_user   order by sums desc limit 20");
        
        return $this->fetch('',[
		    'list'=>$list
		    ]);
        
    }
	
}